package com.entity;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;


import java.io.FileInputStream;
import java.io.IOException;

/**
 * 读取Excel数据
 */

public class ExcelReadTest {
    /**
     * 文件生成路径
     */
    String path = "G:\\Excel练习\\";

    @Test
    //03版本读取操作
    public void testRead03() throws IOException {
        //1.读取文件流
        FileInputStream fileInputStream = new FileInputStream("G:\\Excel练习\\读取用户信息表03.xls");
        //2.获取工作薄`
        Workbook workbook = new HSSFWorkbook(fileInputStream);
        //3.获取工作表
        Sheet sheet = workbook.getSheetAt(0);
        //4.获取行
        Row row = sheet.getRow(2);
        //5.获取列
        Cell cell = row.getCell(2);
        //读取数据一定要注意读取数据的类型
        String numericCellValue = cell.getStringCellValue();
        System.out.println(numericCellValue);
        fileInputStream.close();
    }

    @Test
    //07版本读取操作
    public void testRead07() throws IOException {
        //1.获取文件流
        FileInputStream fileInputStream = new FileInputStream( "G:\\Excel练习\\读取用户信息表07.xlsx");
        //2.获取工作薄
        Workbook workbook = new XSSFWorkbook(fileInputStream);
        //3.获取工作表
        Sheet sheet = workbook.getSheetAt(0);
        //4.获取行
        Row row = sheet.getRow(2);
        //5.获取列
        Cell cell = row.getCell(1);
        //读取数据一定要注意读取数据的类型
        String numericeCellValue = cell.getStringCellValue();
        System.out.println(numericeCellValue);
        fileInputStream.close();
    }

}
